package com.neiquan.neiquandemo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neiquan.neiquandemo.R;
import com.neiquan.neiquandemo.test.FourFragment;
import com.neiquan.neiquandemo.test.OneFragment;
import com.neiquan.neiquandemo.test.ThreeFragment;
import com.neiquan.neiquandemo.test.TwoFragment;

import net.neiquan.applibrary.base.BaseActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 作者 ： dyj
 * 时间 ： 2016/12/29.
 */

public class MainActivity extends BaseActivity {

    @InjectView(R.id.tab_home)
    LinearLayout home_lay;
    @InjectView(R.id.tab_talent)
    LinearLayout sort_lay;
    @InjectView(R.id.tab_how)
    LinearLayout shop_lay;
    @InjectView(R.id.tab_me)
    LinearLayout mine_lay;
    private int preId[] = {R.mipmap.tab_1_h, R.mipmap.tab_2_h, R.mipmap.tab_3_h, R.mipmap.tab_4_h};
    private int norId[] = {R.mipmap.tab_1_n, R.mipmap.tab_2_n, R.mipmap.tab_3_n, R.mipmap.tab_4_n};
    private ViewGroup Ly[] ;
    private FragmentTransaction fragmentTransaction;
    private Fragment fragments[] = {new OneFragment(), new TwoFragment(), new ThreeFragment(), new FourFragment()};

    @Override
    public int getRootViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void setViews() {
        Ly = new ViewGroup[]{home_lay, sort_lay, shop_lay, mine_lay};
        setDefaultFragment(true, 0);
    }

    @Override
    public void initData() {

    }

    private int position = 0;
    public void onTabClicked(View view) {
        switch (view.getId()) {
            case R.id.tab_home:
                position = 0;
                break;
            case R.id.tab_talent:
                position = 1;
                break;
            case R.id.tab_how:
                position = 2;
                break;
            case R.id.tab_me:
                position = 3;
                break;
            default:
                break;
        }
        setTabSelect(position);
        setDefaultFragment(false, position);
    }

    private void setTabSelect(int position) {
        for (int i = 0; i < Ly.length; i++) {
            if (position == i) {
                ((ImageView) Ly[i].getChildAt(0)).setImageResource(preId[i]);
                ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.colorAccent));
            } else {
                ((ImageView) Ly[i].getChildAt(0)).setImageResource(norId[i]);
                ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.colorPrimary));
            }
        }
    }

    private void setDefaultFragment(boolean first, int index) {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (first) {
            fragmentTransaction.add(R.id.framLayoutId, fragments[0]);
            fragmentTransaction.add(R.id.framLayoutId, fragments[1]);
            fragmentTransaction.add(R.id.framLayoutId, fragments[2]);
            fragmentTransaction.add(R.id.framLayoutId, fragments[3]);
        }
        fragmentTransaction.hide(fragments[0]);
        fragmentTransaction.hide(fragments[1]);
        fragmentTransaction.hide(fragments[2]);
        fragmentTransaction.hide(fragments[3]);
        fragmentTransaction.show(fragments[index]);
        fragmentTransaction.commit();
    }

}
